home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / oort / radar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.5 KB  |  374 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*------------------------------------------------------------------------------
  18.  *
  19.  * OORT - radar.c - Routines for displaying the radar.
  20.  *
  21.  * $Id: radar.c,v 1.2 1994/01/28 00:20:51 mtj Exp $
  22.  * 
  23.  * Chris Fouts - May, 1993.
  24.  * 
  25.  *----------------------------------------------------------------------------*/
  26. #include <stdio.h>
  27. #include <gl.h>
  28.  
  29. #include "oort.h"
  30. #include "radar.h"
  31.  
  32. /* BEGIN PROTOTYPES -S radar.c */
  33. /* END PROTOTYPES -S radar.c */
  34.  
  35.  
  36. static Object    radarObj ;
  37. static float    radarAlt = (float)( 2.0f * ASTEROID_RADIUS ) ;
  38. static float    radarMags[] = {
  39.             0.10f, 0.25f, 0.5f, 1.0f, 2.0f,
  40.             } ;
  41. static int    radarSetting = 4 ;
  42.  
  43. extern pfVec3    teamColor[] ;
  44. extern int    debugOn ;
  45. extern int    selfStatus ;
  46. extern int    numberPlayers ;
  47. extern float    longitude ;
  48. extern float    latitude ;
  49. extern pfVec3    up ;
  50. extern pfVec3    ahead ;
  51. extern pfVec3    right ;
  52. extern Motion    motion ;
  53. extern ViewPort    radarVP ;
  54. extern Player    player[] ;
  55.  
  56.  
  57.  
  58. /*------------------------------------------------------------------------------
  59.  * Create the terrain background object.
  60.  *----------------------------------------------------------------------------*/
  61. void
  62. makeRadarBackground(
  63.     int    lats,
  64.     int    lngs
  65.     )
  66. {
  67.     int    i ;
  68.     int    j ;
  69.     float    phi ;
  70.     float    theta ;
  71.     float    sp ;
  72.     float    cp ;
  73.     float    st ;
  74.     float    ct ;
  75.     float    r = ASTEROID_RADIUS ;
  76.     pfVec3    v ;
  77.  
  78.     radarObj = genobj() ;
  79.  
  80.     makeobj( radarObj ) ;
  81.         cpack( 0x0 ) ;
  82.         clear() ;
  83.         cpack( 0x00ff0000 ) ;
  84.         for( i = 1 ; i < lngs ; i++ )
  85.         {
  86.             theta = 360.0f * i / (float)lngs ;
  87.             pfSinCos( theta, &st, &ct ) ;
  88.             bgnline() ;
  89.             for( j = 0 ; j < lats ; j++ )
  90.             {
  91.                 phi = 180.0f * j / ( lats - 1.0f ) ;
  92.                 pfSinCos( phi, &sp, &cp ) ;
  93.                 PFSET_VEC3( v, r*sp*st, -r*sp*ct, r*cp ) ;
  94.                 v3f( v ) ;
  95.             }
  96.             endline() ;
  97.         }
  98.         for( j = 1 ; j < lats - 1 ; j++ )
  99.         {
  100.             if( j != ( lats - 1 ) / 2 )
  101.             {
  102.                 phi = 180.0f * j / ( lats - 1.0f ) ;
  103.                 pfSinCos( phi, &sp, &cp ) ;
  104.                 bgnline() ;
  105.                 for( i = 0 ; i <= lngs ; i++ )
  106.                 {
  107.                     theta = 360.0f * i / (float)lngs ;
  108.                     pfSinCos( theta, &st, &ct ) ;
  109.                     PFSET_VEC3( v, r*sp*st, -r*sp*ct, r*cp );
  110.                     v3f( v ) ;
  111.                 }
  112.                 endline() ;
  113.             }
  114.         }
  115.         cpack( 0x00ffff00 ) ;
  116.         st = 0.0f ;
  117.         ct = 1.0f ;
  118.         bgnline() ;
  119.         for( j = 0 ; j < lats ; j++ )
  120.         {
  121.             phi = 180.0f * j / ( lats - 1.0f ) ;
  122.             pfSinCos( phi, &sp, &cp ) ;
  123.             PFSET_VEC3( v, r*sp*st, -r*sp*ct, r*cp ) ;
  124.             v3f( v ) ;
  125.         }
  126.         endline() ;
  127.         sp = 1.0f ;
  128.         cp = 0.0f ;
  129.         bgnline() ;
  130.         for( i = 0 ; i <= lngs ; i++ )
  131.         {
  132.             theta = 360.0f * i / (float)lngs ;
  133.             pfSinCos( theta, &st, &ct ) ;
  134.             PFSET_VEC3( v, r*sp*st, -r*sp*ct, r*cp ) ;
  135.             v3f( v ) ;
  136.         }
  137.         endline() ;
  138.     closeobj() ;
  139. }
  140.  
  141.  
  142.  
  143. /*------------------------------------------------------------------------------
  144.  * Show the radar.
  145.  *----------------------------------------------------------------------------*/
  146. void
  147. showRadar(
  148.     void
  149.     )
  150. {
  151.     int        i ;
  152.     float        ada ;
  153.     float        zFar ;
  154.     float        l ;
  155.     float        st ;
  156.     float        ct ;
  157.     float        sp ;
  158.     float        cp ;
  159.     pfVec3        a ;    /* radar ahead (looking down) */
  160.     pfVec3        u ;    /* radar up (looking forward) */
  161.     pfVec3        r ;    /* radar right (looking right) */
  162.     pfVec3        loc ;
  163.     pfMatrix    m ;
  164.     pfMatrix    invRot ;
  165.     pfVec3        blip[4] ;
  166.     pfVec2        c ;
  167.  
  168.     if( selfStatus == OORT_ST_ON_GROUND )
  169.     {
  170.         PFSCALE_VEC3( a, -1.0f, motion.cg ) ;
  171.         pfNormalizeVec3( a ) ;
  172.  
  173.         ada = PFDOT_VEC3( ahead, a ) ;
  174.  
  175.         /*
  176.          * If ahead vector is either pointing straight up or down, key
  177.          * off of up vector.
  178.          */
  179.         if( ada < -0.9999f && 0.9999f < ada )
  180.         {
  181.             ADDSCALE_VEC3( u, up, -PFDOT_VEC3( up, a ), a ) ;
  182.             pfNormalizeVec3( u ) ;
  183.         }
  184.         /*
  185.          * otherwise, use ahead vector.
  186.          */
  187.         else
  188.         {
  189.             ADDSCALE_VEC3( u, ahead, -ada, a ) ;
  190.             pfNormalizeVec3( u ) ;
  191.         }
  192.         pfCrossVec3( r, a, u ) ;
  193.  
  194.         PFSCALE_VEC3( loc, ASTEROID_RADIUS, a ) ;
  195.     }
  196.     else
  197.     {
  198.         pfSinCos( longitude, &st, &ct ) ;
  199.         pfSinCos( latitude, &sp, &cp ) ;
  200.         PFSET_VEC3( a, -cp*st, cp*ct, -sp ) ;
  201.         PFSET_VEC3( r, ct, st, 0.0f ) ;
  202.         pfCrossVec3( u, r, a ) ;
  203.  
  204.         PFSET_VEC3( loc, -cp*st, cp*ct, -sp ) ;
  205.         PFSCALE_VEC3( loc, ASTEROID_RADIUS, loc ) ;
  206.     }
  207.  
  208.     /*
  209.      * Rotation and height of view matrix.
  210.      */
  211.     m[0][0] =  r[0] ; m[1][0] =  r[1] ; m[2][0] =  r[2] ;
  212.     m[0][1] =  u[0] ; m[1][1] =  u[1] ; m[2][1] =  u[2] ;
  213.     m[0][2] = -a[0] ; m[1][2] = -a[1] ; m[2][2] = -a[2] ;
  214.     m[3][0] =  0.0f ; m[3][1] =  0.0f ; m[3][2] = -radarAlt ;
  215.     m[0][3] =  0.0f ; m[1][3] =  0.0f ; m[2][3] =  0.0f ; m[3][3] = 1.0f ;
  216.  
  217.     /*
  218.      * Inverse rotation matrix (keeps radar blips orthogonal to line
  219.      * of sight).
  220.      */
  221.     invRot[0][0] = r[0] ; invRot[0][1] = r[1] ; invRot[0][2] = r[2] ;
  222.     invRot[1][0] = u[0] ; invRot[1][1] = u[1] ; invRot[1][2] = u[2] ;
  223.     invRot[2][0] = -a[0] ; invRot[2][1] = -a[1] ; invRot[2][2] = -a[2] ;
  224.     invRot[3][0] = 0.0f ; invRot[3][1] = 0.0f ; invRot[3][2] = 0.0f ;
  225.     invRot[0][3] = 0.0f ; invRot[1][3] = 0.0f ; invRot[2][3] = 0.0f ;
  226.     invRot[3][3] = 1.0f ;
  227.  
  228.     /*
  229.      * Calculate size of radar blip so it's always the same size.
  230.      */
  231.     l = radarAlt * (float)( 2.0f*CAR_LENGTH / ( 0.25f*ASTEROID_RADIUS ) ) ;
  232.  
  233.     PFSET_VEC3( blip[0], -l, -l, 0.0f ) ;
  234.     PFSET_VEC3( blip[1],  l, -l, 0.0f ) ;
  235.     PFSET_VEC3( blip[2], -l,  l, 0.0f ) ;
  236.     PFSET_VEC3( blip[3],  l,  l, 0.0f ) ;
  237.  
  238.     /*
  239.      * Rotate blip so it always faces us.
  240.      */
  241.     pfXformVec3( blip[0], blip[0], invRot ) ;
  242.     pfXformVec3( blip[1], blip[1], invRot ) ;
  243.     pfXformVec3( blip[3], blip[3], invRot ) ;
  244.     pfXformVec3( blip[2], blip[2], invRot ) ;
  245.  
  246.     viewport( radarVP.left, radarVP.right, radarVP.bottom,
  247.         radarVP.top ) ;
  248.  
  249.     /*
  250.      * Calculate zFar such that we only see the surface of the asteroid
  251.      * facing us.
  252.      */
  253.     zFar = radarAlt + ASTEROID_RADIUS ;
  254.     zFar = zFar - (float)( ASTEROID_RADIUS * ASTEROID_RADIUS ) / zFar ;
  255.     perspective( 450, 1.0f, 1.0f, zFar ) ;
  256.  
  257.     loadmatrix( m ) ;
  258.     translate( loc[0], loc[1], loc[2] ) ;
  259.  
  260.     callobj( radarObj ) ;
  261.  
  262.     /*
  263.      * Draw self on radar as open square.
  264.      */
  265.     if( selfStatus == OORT_ST_ON_GROUND )
  266.     {
  267.         c3f( teamColor[player[SELF].team] ) ;
  268.         pushmatrix() ;
  269.         translate( player[SELF].xyz[0], player[SELF].xyz[1],
  270.                 player[SELF].xyz[2] ) ;
  271.         bgnclosedline() ;
  272.             v3f( blip[0] ) ;
  273.             v3f( blip[1] ) ;
  274.             v3f( blip[3] ) ;
  275.             v3f( blip[2] ) ;
  276.         endclosedline() ;
  277.         popmatrix() ;
  278.     }
  279.  
  280.     /*
  281.      * Draw enemies as filled squares.
  282.      */
  283.     for( i = ENEMY ; i < numberPlayers ; i++ )
  284.     {
  285.         if( !EXPLODING( player+i ) && !CLOAKED( player+i ) )
  286.         {
  287.             c3f( teamColor[player[i].team] ) ;
  288.             pushmatrix() ;
  289.             translate( player[i].xyz[0], player[i].xyz[1],
  290.                     player[i].xyz[2] ) ;
  291.             bgntmesh() ;
  292.                 v3f( blip[0] ) ;
  293.                 v3f( blip[1] ) ;
  294.                 v3f( blip[2] ) ;
  295.                 v3f( blip[3] ) ;
  296.             endtmesh() ;
  297.             popmatrix() ;
  298.         }
  299.     }
  300.  
  301.     /*
  302.      * Draw cross hairs if in orbit.
  303.      */
  304.     if( selfStatus == OORT_ST_IN_ORBIT )
  305.     {
  306.         ortho2( -1.0f, 1.0f, -1.0f, 1.0f ) ;
  307.         pfPushIdentMatrix() ;
  308.         cpack( 0x0000ffff ) ;
  309.         bgnline() ;
  310.             c[0] = 0.0f ;
  311.             c[1] = 0.1f ;
  312.             v2f( c ) ;
  313.             c[1] = -0.1f ;
  314.             v2f( c ) ;
  315.         endline() ;
  316.         bgnline() ;
  317.             c[0] = 0.1f ;
  318.             c[1] = 0.0f ;
  319.             v2f( c ) ;
  320.             c[0] = -0.1f ;
  321.             v2f( c ) ;
  322.         endline() ;
  323.         pfPopMatrix() ;
  324.     }
  325. }
  326.  
  327.  
  328.  
  329. /*------------------------------------------------------------------------------
  330.  * Zoom the radar view in.
  331.  *----------------------------------------------------------------------------*/
  332. void
  333. radarZoomIn(
  334.     void
  335.     )
  336. {
  337.     if( radarSetting > 0 )
  338.     {
  339.         radarSetting-- ;
  340.         radarAlt = radarMags[radarSetting] * ASTEROID_RADIUS ;
  341.     }
  342. }
  343.  
  344.  
  345.  
  346. /*------------------------------------------------------------------------------
  347.  * Zoom the radar view out.
  348.  *----------------------------------------------------------------------------*/
  349. void
  350. radarZoomOut(
  351.     void
  352.     )
  353. {
  354.     if( radarSetting < ( sizeof(radarMags) / sizeof(radarMags[0]) ) - 1 )
  355.     {
  356.         radarSetting++ ;
  357.         radarAlt = radarMags[radarSetting] * ASTEROID_RADIUS ;
  358.     }
  359. }
  360.  
  361.  
  362.  
  363. /*------------------------------------------------------------------------------
  364.  * Set radar setting to maximum range.
  365.  *----------------------------------------------------------------------------*/
  366. void
  367. radarMaxRange(
  368.     void
  369.     )
  370. {
  371.     radarSetting = ( sizeof(radarMags) / sizeof(radarMags[0]) ) - 1 ;
  372.     radarAlt = radarMags[radarSetting] * ASTEROID_RADIUS ;
  373. }
  374.